home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 …ember: Reference Library / Dev.CD Dec 00 RL Disk 1.toast / pc / technical documentation / develop / develop issue 26 / develop issue 26 code / wide (64 bit) library / sources / testwide.c next >
Encoding:
C/C++ Source or Header  |  1996-07-18  |  13.7 KB  |  520 lines

  1. #ifdef __MWERKS__
  2. #include <console.h>
  3. #endif
  4. #include <stdio.h>
  5.  
  6. #include "Wide.h"
  7.  
  8. /* forward references */
  9. void tst_printint( wide *big_ptr );
  10. void tst_print3( wide *big1_ptr, wide *big2_ptr, wide *big3_ptr );
  11. void tst_compare3( wide *big1_ptr, wide *big2_ptr, wide *big3_ptr,
  12.                wide *save1_ptr, wide *save2_ptr, wide *save3_ptr, short equal_test );
  13. void tst_wide( wide *big_ptr, long hi, long lo, char *strp );
  14. void __assertprint(char *filenam_ptr, char *linenum_ptr, char *expression_ptr);
  15. void __assert_custom(char *filenam_ptr, char *linenum_ptr, char *expression_ptr);
  16. void tst_long( long actual, long expected, char *strp );
  17.  
  18.  
  19.  
  20.  
  21. main()
  22. {
  23. short    i;
  24. short    tst1, tst2, tst3;
  25. wide    big1, big2, big3;
  26. wide    save1, save2, save3;
  27. wide    *resultp, *resultAp, *resultBp;
  28. decimal dec_str;
  29. long    result, remainder;
  30.  
  31.         wide    wide1;
  32.         decimal dec;
  33.  
  34.  
  35.         wide1.hi = 0;
  36.         wide1.lo = 0x00000401;
  37.         WideShift(&wide1, 3);
  38.         
  39.         wide1.hi = 0;
  40.         wide1.lo = 0x00000C;
  41.         WideShift(&wide1, 3); // Should be 2 because of rounding
  42.  
  43.  
  44.         wide1.hi = 0;
  45.         wide1.lo = 0;
  46.         WideToDecStr(&dec, &wide1);     /* should be "0" */
  47.  
  48.         wide1.hi = 0;
  49.         wide1.lo = 1;
  50.         WideToDecStr(&dec, &wide1);     /* should be "1" */
  51.  
  52.         WideNegate( &wide1 );
  53.         WideToDecStr(&dec, &wide1);     /* should be Negative "1" */
  54.  
  55.         wide1.hi = 0;
  56.         wide1.lo = 12345;
  57.         WideToDecStr(&dec, &wide1);     /* should be "12345" */
  58.  
  59.         WideNegate( &wide1 );
  60.         WideToDecStr(&dec, &wide1);     /* should be Negative "12345" */
  61.  
  62.         wide1.hi = 1;
  63.         wide1.lo = 0;
  64.         WideToDecStr(&dec, &wide1);     /* should be "4294967296" */
  65.  
  66.         WideNegate( &wide1 );
  67.         WideToDecStr(&dec, &wide1);     /* should be Negative "4294967296" */
  68.  
  69.  
  70.     resultp = WideAssign32( &big1, -1 );
  71.     WideAssign32( &big2, 0 );
  72.     WideAssign32( &big3, 25 );
  73.     save1 = big1;
  74.     save2 = big2;
  75.     save3 = big3;
  76.     printf( "WideAssign32() = " );
  77.     tst_print3( &big1, &big2, &big3 );
  78.  
  79.     WideAdd32( &big1, 3 );
  80.     resultAp = WideAdd32( &big2, -1 );
  81.     WideAdd32( &big3, -30 );
  82.     printf( "WideAdd32() = " );
  83.     tst_print3( &big1, &big2, &big3 );
  84.  
  85.     WideSubtract32( &big1, 3 );
  86.     WideSubtract32( &big2, -1 );
  87.     resultBp = WideSubtract32( &big3, -30 );
  88.     printf( "WideSubtract32() = " );
  89.     tst_print3( resultp, resultAp, resultBp );
  90.     tst_compare3( resultp,resultAp, resultBp, &save1, &save2, &save3, true );
  91.  
  92.     WideAssign32( &big1, 0x40000000 );
  93.     printf( "WideAssign32() = " );
  94.     tst_printint( &big1 ); printf( "\n" );
  95.     WideBitShift( &big1, -2 );
  96.     printf( "WideBitShift(left twice) = " );
  97.     tst_printint( &big1 ); printf( "\n" );
  98.     WideAdd32( &big1, 3507 );
  99.     printf( "WideAdd32(3507)\n\n" );
  100.     WideToDecStr( &dec_str, &big1 );
  101.     if( (dec_str.sgn == 0) &&
  102.         (dec_str.exp == 0) &&
  103.         (dec_str.sig.length == 10) &&
  104.         (dec_str.sig.text[0] == '4') &&
  105.         (dec_str.sig.text[1] == '2') &&
  106.         (dec_str.sig.text[2] == '9') &&
  107.         (dec_str.sig.text[3] == '4') &&
  108.         (dec_str.sig.text[4] == '9') &&
  109.         (dec_str.sig.text[5] == '7') &&
  110.         (dec_str.sig.text[6] == '0') &&
  111.         (dec_str.sig.text[7] == '8') &&
  112.         (dec_str.sig.text[8] == '0') &&
  113.         (dec_str.sig.text[9] == '3') )
  114.         printf( "PASS" );
  115.     else
  116.         printf( "FAIL" );
  117.     printf( ": " );
  118.     tst_printint( &big1 );
  119.     printf( ", WideToDecStr() with more than 32 bits\n\n" );
  120.  
  121.     WideAssign32( &big1, -10 );
  122.     WideAssign32( &big2, 0x7FFFFFF0 );
  123.     WideAssign32( &big3, 0x7FFFFFFF );
  124.     save1 = big1;
  125.     save2 = big2;
  126.     save3 = big3;
  127.     printf( "WideAssign32() = " );
  128.     tst_print3( &big1, &big2, &big3 );
  129.  
  130.     WideAdd32( &big3, 1 );
  131.     resultp = (wide *)WideAdd( &big3, &big1 );
  132.     for( i=0; i < 20; ++i ) WideAdd( &big3, &big2 );
  133.     printf( "WideAdd() = " );
  134.     tst_print3( &big1, &big2, resultp );
  135.     tst_compare3( &big1, &big2, resultp, &save1, &save2, &save3, false );
  136.  
  137.     /*------------------------------*/
  138.     printf( "<<< press ENTER for more >>>\n\n" );
  139.     getchar();
  140.  
  141.     WideSubtract32( &big3, 1 );
  142.     resultp = (wide *)WideSubtract( &big3, &big1 );
  143.     for( i=0; i < 20; ++i ) WideSubtract( &big3, &big2 );
  144.  
  145.     printf( "WideSubtract() = " );
  146.     tst_print3( &big1, &big2, resultp );
  147.     tst_compare3( &big1, &big2, resultp, &save1, &save2, &save3, true );
  148.  
  149.     WideAssign32( &big1, 0 );
  150.     WideAssign32( &big2, 7 );
  151.     WideAssign32( &big3, 0x7FFFFFFF );
  152.     save1 = big1;
  153.     save2 = big2;
  154.     save3 = big3;
  155.     printf( "WideAssign32() = " );
  156.     tst_print3( &big1, &big2, &big3 );
  157.  
  158.     WideNegate(  &big1 );
  159.     WideNegate(  &big2 );
  160.     WideNegate(  &big3 );
  161.     printf( "WideNegate() = " );
  162.     tst_print3( &big1, &big2, &big3 );
  163.     printf( "\n" );
  164.  
  165.     WideAssign32( &big3, 1 );
  166.     save3 = big3;
  167.     WideAdd( &big2, &big3 );
  168.     if( (big2.lo == -6) && (big2.hi == -1) )
  169.         printf( "PASS" );
  170.     else
  171.         printf( "FAIL" );
  172.     printf( ": adding a small positive to a small negative\n" );
  173.     WideSubtract( &big2, &big3 );
  174.     if( (big3.lo == save3.lo) && (big3.hi == save3.hi) )
  175.         printf( "PASS" );
  176.     else
  177.         printf( "FAIL" );
  178.     printf( ": subtracting a small positive to a small negative\n\n" );
  179.  
  180.     WideAssign32( &big1, 18 );
  181.     result = WideDivide( &big1, 4, &remainder );
  182.     if( (result == 4) && (remainder == 2) )
  183.         printf( "PASS" );
  184.     else
  185.         printf( "FAIL" );
  186.     printf( ": %ld, %ld, WideDivide()\n",
  187.             result, remainder );
  188.  
  189.     WideAssign32( &big1, 10000 );
  190.     result = WideDivide( &big1, 200, &remainder );
  191.     if( (result == 50) && (remainder == 0) )
  192.         printf( "PASS" );
  193.     else
  194.         printf( "FAIL" );
  195.     printf( ": %ld, %ld, WideDivide()\n",
  196.             result, remainder );
  197.  
  198.     WideMultiply( 10, 307, &big1 );
  199.     result = WideDivide( &big1, 307, &remainder );
  200.     if( (result == 10) && (remainder == 0) )
  201.         printf( "PASS" );
  202.     else
  203.         printf( "FAIL" );
  204.     printf( ": %ld, %ld, WideMultiply() WideDivide(), small positive num.\n",
  205.             result, remainder );
  206.  
  207.     WideMultiply( -6, -3, &big1 );
  208.     result = WideDivide( &big1, -4, &remainder );
  209.     if( (result == -4) && (remainder == 2) )
  210.         printf( "PASS" );
  211.     else
  212.         printf( "FAIL" );
  213.     printf( ": %ld, %ld, WideMultiply() WideDivide(), small negative num.\n",
  214.              result, remainder );
  215.  
  216.     WideMultiply( 0x7FFFFFF3, 0x5A5A, &big1 );
  217.     result = WideDivide( &big1, 0x7FFFFFF3, &remainder );
  218.     if( (result == 0x5A5A) && (remainder == 0) )
  219.         printf( "PASS" );
  220.     else
  221.         printf( "FAIL" );
  222.     printf( ": 0x%lx, %ld, WideMultiply() WideDivide()\n",
  223.              result, remainder );
  224.  
  225.     WideMultiply( -(0x7FFFFFF3), 0x5A5A, &big1 );
  226.     result = WideDivide( &big1, 0x7FFFFFF3, &remainder );
  227.     if( (result == -(0x5A5A)) && (remainder == 0) )
  228.         printf( "PASS" );
  229.     else
  230.         printf( "FAIL" );
  231.     printf( ": 0x%lx, %ld, WideMultiply() WideDivide()\n",
  232.              result, remainder );
  233.  
  234.     /*------------------------------*/
  235.     printf( "<<< press ENTER for more >>>\n\n" );
  236.     getchar();
  237.  
  238.     WideAssign32( &big1, 1 );
  239.     result = WideDivide( &big1, 0, NULL );
  240.     if( result == gxPositiveInfinity )
  241.         printf( "PASS" );
  242.     else
  243.         printf( "FAIL" );
  244.     printf( ": %lx, WideDivide() positive by 0\n", result );
  245.  
  246.     WideAssign32( &big1, -1 );
  247.     result = WideDivide( &big1, 0, NULL );
  248.     if( result == gxNegativeInfinity )
  249.         printf( "PASS" );
  250.     else
  251.         printf( "FAIL" );
  252.     printf( ": %lx, WideDivide() negative by 0\n\n", result );
  253.  
  254.     resultp = (wide *)WideMultiply( 0, 0, &big1 );
  255.     WideMultiply( -4, -(0x7FFFFFFF), &big2 );
  256.     WideMultiply( -(0x7FFFFFFF), 0x7FFFFFFF, &big3 );
  257.     save1 = big1;
  258.     save2 = big2;
  259.     save3 = big3;
  260.     printf( "WideMultiply(), 0 * 0, -4 * -(0x7FFFFFFF), -(0x7FFFFFFF) * 0x7FFFFFFF =\n" );
  261.     tst_print3( resultp, &big2, &big3 );
  262.  
  263.     WideNegate(  &big1 );
  264.     WideNegate(  &big2 );
  265.     WideNegate(  &big3 );
  266.     printf( "WideNegate() = " );
  267.     tst_print3( &big1, &big2, &big3 );
  268.     printf( "\n" );
  269.  
  270.     result = WideDivide( &big2, -4, NULL );
  271.     if( result == 0x7FFFFFFF )
  272.         printf( "PASS" );
  273.     else
  274.         printf( "FAIL" );
  275.     printf( ": WideDivide() = 0x%lx\n", result );
  276.  
  277.     WideNegate(  &big3 );
  278.     result = WideDivide( &big3, 0x7FFFFFFF, NULL );
  279.     if( result == -(0x7FFFFFFF) )
  280.         printf( "PASS" );
  281.     else
  282.         printf( "FAIL" );
  283.     printf( ": WideDivide() = 0x%lx\n", result );
  284.  
  285.     WideAssign32( &big1, 25 );
  286.     resultp = (wide *)WideWideDivide( &big1, 3, &remainder );
  287.     if( (resultp->lo == 8) && (resultp->hi == 0) && (remainder == 1) )
  288.         printf( "PASS" );
  289.     else
  290.         printf( "FAIL" );
  291.     printf( ": %ld, %ld, WideWideDivide()\n",
  292.             resultp->lo, remainder );
  293.  
  294.     resultp = (wide *)WideMultiply( 32, 0x7FFFFFFF, &big2 );
  295.     tst_wide( resultp, 0xF, 0xFFFFFFE0, "WideMultiply()" );
  296.  
  297.     resultp = (wide *)WideWideDivide( resultp, 4, &remainder );
  298.     if( (resultp->lo == 0xFFFFFFF8) && (resultp->hi == 3) && (remainder == 0) )
  299.         printf( "PASS" );
  300.     else
  301.         printf( "FAIL" );
  302.     printf( ": 0x%lx, 0x%lx, %ld, WideWideDivide()\n",
  303.             resultp->hi, resultp->lo, remainder );
  304.  
  305.     resultp = (wide *)WideWideDivide( resultp, 16, &remainder );
  306.     if( (resultp->lo == 0x3FFFFFFF) && (resultp->hi == 0) && (remainder == 8) )
  307.         printf( "PASS" );
  308.     else
  309.         printf( "FAIL" );
  310.     printf( ": 0x%lx, 0x%lx, %ld, WideWideDivide()\n",
  311.             resultp->hi, resultp->lo, remainder );
  312.  
  313.     WideAssign32( &big1, 0x7FFFFFFF );
  314.     resultp = (wide *)WideBitShift( &big1, -2 );
  315.     tst_wide( resultp, 1, 0xFFFFFFFC, "WideBitShift(pos, -2)" );
  316.  
  317.     resultp = (wide *)WideBitShift( &big1, 1 );
  318.     tst_wide( resultp, 0, 0xFFFFFFFE, "WideBitShift(pos, 1)" );
  319.  
  320.     WideAssign32( &big1, -10 );
  321.     resultp = (wide *)WideBitShift( &big1, 1 );
  322.     tst_wide( resultp, -1, -5, "WideBitShift(neg, 1)" );
  323.  
  324.     big1.hi = 0x80000000;
  325.     big1.lo = 0;
  326.     resultp = (wide *)WideBitShift( &big1, -1 );
  327.     tst_wide( resultp, 0, 0, "WideBitShift(max neg, -1)" );
  328.  
  329.     big1.hi = 0x80000000;
  330.     big1.lo = 0;
  331.     resultp = (wide *)WideBitShift( &big1, 1 );
  332.     tst_wide( resultp, 0xC0000000, 0, "WideBitShift(max neg, 1)" );
  333.  
  334.     big1.hi = 0;
  335.     big1.lo = 3;
  336.     resultp = (wide *)WideShift( &big1, 1 );
  337.     tst_wide( resultp, 0, 2, "WideShift(pos, 1)" );
  338.  
  339.     big1.hi = 1;
  340.     big1.lo = 0xFFFFFFFF;
  341.     resultp = (wide *)WideShift( &big1, 1 );
  342.     tst_wide( resultp, 1, 0, "WideShift(pos, 1)" );
  343.     resultp = (wide *)WideShift( &big1, -1 );
  344.     tst_wide( resultp, 2, 0, "WideShift(pos, -1)" );
  345.  
  346.     /*------------------------------*/
  347.     printf( "<<< press ENTER for more >>>\n\n" );
  348.     getchar();
  349.  
  350.     big1.hi = 3;
  351.     big1.lo = 0xC0000000;
  352.     resultp = (wide *)WideBitShift( &big1, 33 );
  353.     tst_wide( resultp, 0, 1, "WideBitShift(, 33)" );
  354.  
  355.     big1.hi = 3;
  356.     big1.lo = 0xC0000000;
  357.     resultp = (wide *)WideBitShift( &big1, -33 );
  358.     tst_wide( resultp, 0x80000000, 0, "WideBitShift(, -33)" );
  359.  
  360.     big1.hi = 1;
  361.     big1.lo = 1;
  362.     resultp = (wide *)WideShift( &big1, -1 );
  363.     tst_wide( resultp, 2, 2, "WideShift(pos, -1)" );
  364.  
  365.     big1.hi = -1;
  366.     big1.lo = -20;
  367.     resultp = (wide *)WideShift( &big1, 2 );
  368.     tst_wide( resultp, -1, -5, "WideShift(neg, 2)" );
  369.     resultp = (wide *)WideShift( &big1, 1 );
  370.     tst_wide( resultp, -1, -2, "WideShift(neg, 1)" );
  371.  
  372.     big1.hi = 0x80000000;
  373.     big1.lo = 0;
  374.     resultp = (wide *)WideShift( &big1, -1 );
  375.     tst_wide( resultp, 0, 0, "WideShift(max neg, -1)" );
  376.  
  377.     big1.hi = 0x80000000;
  378.     big1.lo = 0;
  379.     resultp = (wide *)WideShift( &big1, 1 );
  380.     tst_wide( resultp, 0xC0000000, 0, "WideShift(max neg, 1)" );
  381.  
  382.     big1.hi = 0;
  383.     big1.lo = 0x0000000C;
  384.     resultp = (wide *)WideShift( &big1, 3 );
  385.     tst_wide( resultp, 0, 2, "WideShift Rounding" );
  386.  
  387.     big1.hi = 0x00000001;
  388.     big1.lo = 0x80000000;
  389.     resultp = (wide *)WideShift( &big1, 32 );
  390.     tst_wide( resultp, 0, 2, "WideShift Rounding" );
  391.  
  392.     big1.hi = 0x00000001;
  393.     big1.lo = 0x00000000;
  394.     resultp = (wide *)WideShift( &big1, 32 );
  395.     tst_wide( resultp, 0, 1, "WideShift Rounding" );
  396.  
  397.     big1.hi = 0;
  398.     big1.lo = 9;
  399.     result = WideSquareRoot( &big1 );
  400.     tst_long( result, 3, "WideSquareRoot(0,9)" );
  401.  
  402.     big1.hi = 0;
  403.     big1.lo = 0;
  404.     result = WideSquareRoot( &big1 );
  405.     tst_long( result, 0, "WideSquareRoot(0,0)" );
  406.  
  407.     big1.hi = 4;
  408.     big1.lo = 0;
  409.     result = WideSquareRoot( &big1 );
  410.     tst_long( result, 0x20000, "WideSquareRoot(4,0)" );
  411.  
  412.     big1.hi = 0x40000000;
  413.     big1.lo = 0;
  414.     result = WideScale( &big1 );
  415.     tst_long( result, 62, "WideScale()" );
  416.  
  417.     return 0;
  418. }
  419.  
  420. void tst_print3( wide *big1_ptr, wide *big2_ptr, wide *big3_ptr )
  421. {
  422.     tst_printint( big1_ptr );
  423.     printf( ", " );
  424.     tst_printint( big2_ptr );
  425.     printf( ", " );
  426.     tst_printint( big3_ptr );
  427.     printf( "\n" );
  428. }
  429.  
  430.  
  431. void tst_printint( wide *big_ptr )
  432. {
  433. char *outp;
  434. short  num_dig;
  435. decimal dec_str;
  436.  
  437.  
  438.     WideToDecStr( &dec_str, big_ptr );
  439.  
  440.     num_dig = dec_str.sig.length;
  441.     outp = (char *) &dec_str.sig.text[0];
  442.  
  443.     if( dec_str.sgn != 0 ) printf( "-" );
  444.  
  445.     while( num_dig-- > 0 )
  446.     {
  447.         printf( "%c", *outp++ );
  448.     }
  449. }
  450.  
  451.  
  452. void tst_compare3( wide *big1_ptr, wide *big2_ptr, wide *big3_ptr,
  453.                wide *save1_ptr, wide *save2_ptr, wide *save3_ptr, short equal_test )
  454. {
  455. short    tst1, tst2, tst3;
  456.  
  457.     printf( "\n" );
  458.  
  459.     if( equal_test )
  460.     {
  461.         tst1 = WideCompare( save1_ptr, big1_ptr );
  462.         tst2 = WideCompare( save2_ptr, big2_ptr );
  463.         tst3 = WideCompare( save3_ptr, big3_ptr );
  464.         if( (tst1 == 0) && (tst2 == 0) && (tst3 == 0) )
  465.             printf( "PASS" );
  466.         else
  467.             printf( "FAIL" );
  468.         printf( ": WideAdd32(), WideSubtract32, WideCompare() == 0\n" );
  469.     }
  470.  
  471.     tst1 = WideCompare( big1_ptr, big2_ptr );
  472.     tst2 = WideCompare( big1_ptr, big3_ptr );
  473.     tst3 = WideCompare( big2_ptr, big3_ptr );
  474.     if( (tst1 < 0) && (tst2 < 0) && (tst3 < 0) )
  475.         printf( "PASS" );
  476.     else
  477.         printf( "FAIL" );
  478.     printf( ": %d, %d, %d,  WideCompare() < 0\n", tst1, tst2, tst3 );
  479.  
  480.     tst1 = WideCompare( big3_ptr, big2_ptr );
  481.     tst2 = WideCompare( big3_ptr, big1_ptr );
  482.     tst3 = WideCompare( big2_ptr, big1_ptr );
  483.     if( (tst1 > 0) && (tst2 > 0) && (tst3 > 0) )
  484.         printf( "PASS" );
  485.     else
  486.         printf( "FAIL" );
  487.     printf( ": %d, %d, %d, WideCompare() > 0\n\n", tst1, tst2, tst3 );
  488.  
  489. }
  490.  
  491.  
  492. void tst_wide( wide *big_ptr, long hi, long lo, char *strp )
  493. {
  494.     if( (big_ptr->lo == lo) && (big_ptr->hi == hi) )
  495.          printf( "PASS" );
  496.     else
  497.         printf( "FAIL" );
  498.     printf( ": 0x%lx, 0x%lx, %s\n", big_ptr->hi, big_ptr->lo, strp );
  499. }
  500.  
  501. void tst_long( long actual, long expected, char *strp )
  502. {
  503.     if( actual == expected )
  504.          printf( "PASS" );
  505.     else
  506.         printf( "FAIL" );
  507.     printf( ": actual 0x%lx, expected 0x%lx, %s\n", actual, expected, strp );
  508. }
  509.  
  510. void __assertprint(char *filenam_ptr, char *linenum_ptr, char *expression_ptr)
  511. {
  512.     printf( "--- assert ---\n" );
  513.     printf( "%s, %s, %s\n", filenam_ptr, linenum_ptr, expression_ptr );
  514. }
  515.  
  516. void __assert_custom(char *filenam_ptr, char *linenum_ptr, char *expression_ptr)
  517. {
  518.     __assertprint( filenam_ptr, linenum_ptr, expression_ptr );
  519. }
  520.